package com.sgcc.pda.greendao.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sgcc.pda.greendao.entity.AppBaseInfoDao;
import com.sgcc.pda.greendao.entity.DaoMaster;
import com.sgcc.pda.greendao.entity.SharepreferenceInfoDao;
import com.sgcc.pda.greendao.util.MigrationHelper;

import org.greenrobot.greendao.database.Database;

/**
 * Greendao的SQLiteOpenHelper.
 *
 * @author 王沛栋
 * @data 2018/1/18 15:17
 */

public class DBOpenHelper extends DaoMaster.OpenHelper {
    public DBOpenHelper(Context context, String name) {
        super(context, name);
    }

    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    /**
     * 用户数据库升级
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        Log.e("vvvvvvvvvvvvvvvvv", oldVersion + "--sssssssssssssssssssssss--" + newVersion);
        if (newVersion > oldVersion) {


            // TODO 升级、数据库迁移操作
            // MigrationHelper.migrate(db,AppBaseInfoDao.class, BaseTaskInfoDao.class, MarketDesktopInfoDao.class);
//            MigrationHelper.migrate(db, AppApkInfoDao.class, AppBaseInfoDao.class, BaseTaskInfoDao.class, MarketDesktopInfoDao.class, SharepreferenceInfoDao.class);
            //把需要管理的数据库表DAO作为最后一个参数传入到方法中
            MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {

                        @Override
                        public void onCreateAllTables(Database db, boolean ifNotExists) {
                            DaoMaster.createAllTables(db, ifNotExists);
                        }

                        @Override
                        public void onDropAllTables(Database db, boolean ifExists) {
                            DaoMaster.dropAllTables(db, ifExists);
                        }
                        //DataBeanDao.class
                    }, AppBaseInfoDao.class, SharepreferenceInfoDao.class);


        }
    }
}
